package com.serializable_and_deserialize;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @ClassName: SerializableAndDeserialize
 * @Author: ZhangYue26
 * @Description: 序列化与反序列化的使用
 * @Date: 2020-11-23
 */
public class SerializableAndDeserialize {

    public final static String FILE_PATH = "C:\\Users\\zhangyue26\\Desktop\\test.bin";

    public static void main(String[] args) throws Exception{

        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
        User user = new User();
        user.setAge(23);
        user.setName("发哥发钱啦");
        User.Role role = new User.Role();
        role.setAdmin(true);
        role.setLevel(0);
        role.setRoleName("Admin");
        user.setRole(role);
        outputStream.writeObject(user);
        outputStream.flush();
        outputStream.close();

        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(FILE_PATH));
        User user123 = (User) (inputStream.readObject());
        System.out.println(user123);
        inputStream.close();


    }

}
